Node.js - require ( )


Posted by mijouhsieh on 2023-04-09

對 Node.js 來說:

  • 如果沒有提供檔案路徑, Node.js 讀取它時就會預設是模組。
  • 如果有包括路徑,那 Node.js 就知道這是個檔案。

  • 而對於副檔名,Node.js 是這樣去判斷的:
    不寫副檔名的時候,預設 require 會先把它當成 .js 處理
    如果不是 .js 則會當作 .json 處理
    如果不是 .json,就會當作 .node 處理
    如果都沒辦法讀取,程式就會炸掉

// require packages and file used in the project
const exphbs = require('express-handlebars') //模組
const movieList = require('./movies.json') //檔案

const movieList = require('./movies') //也可簡化,最好寫完整
關於 require 模組與檔案的語法,可以參考 Node 的官方文件


#node.js #require( )







Related Posts

Explore-Binary Search Tree

Explore-Binary Search Tree

圖型識別學習筆記:圖型識別介紹

圖型識別學習筆記:圖型識別介紹

[進階 js 08] Closure

[進階 js 08] Closure


Comments